//
//  QRViewController.m
//  EagleEye
//
//  Created by 18_李明明 on 15-1-20.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "QRViewController.h"
#import "WebQRController.h"
#import "DataBaseHandler.h"
@interface QRViewController ()<UIAlertViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UITextField *content;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIAlertView *alert;
// 开始扫描按钮
@property (nonatomic, strong) UIButton *startScan;
// 执行扫描结果按钮
@property (nonatomic, strong) UIButton *gogogo;
// 数据库
@property (nonatomic, strong) DataBaseHandler *dataBase;
@end

@implementation QRViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataBase = [DataBaseHandler shareInstance];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"扫描二维码";
    // 输入框
    self.content = [[UITextField alloc] initWithFrame:CGRectMake(10, 20, (WIDTH - 20), 40)];
    // 图片
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 70, WIDTH - 20, HEIGHT - 190)];
    [self.imageView setImage:[UIImage imageNamed:@"cow.jpg"]];
    // 开始扫描按钮
    self.startScan = [UIButton buttonWithType:UIButtonTypeSystem];
    self.startScan.frame = CGRectMake(10, HEIGHT - 120, WIDTH - 80, 40);
    
    // Do any additional setup after loading the view.
    [self.startScan setTitle:@"开始扫描" forState:UIControlStateNormal];
    self.content.placeholder = @"也可以手动输入内容";
    self.content.backgroundColor = [UIColor lightGrayColor];
    self.content.borderStyle = UITextBorderStyleRoundedRect;
    self.content.alpha = 0.5;
    [self.view addSubview:self.content];
    [self.view addSubview:self.startScan];
    [self.view addSubview:self.imageView];
    [self.startScan addTarget:self action:@selector(targetScan:) forControlEvents:UIControlEventTouchUpInside];
    // 前往按钮
    self.gogogo = [UIButton buttonWithType:UIButtonTypeSystem];
    self.gogogo.frame = CGRectMake(WIDTH - 60, HEIGHT - 120, 40, 40);
    [self.gogogo setTitle:@"前往" forState:UIControlStateNormal];
    [self.view addSubview:self.gogogo];
    [self.gogogo addTarget:self action:@selector(targetGo:) forControlEvents:UIControlEventTouchUpInside];
    
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"cancle.png"] style:UIBarButtonItemStylePlain target:self action:@selector(targetCancle:)];
}
- (void)targetCancle:(UIButton *)cancle
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.content resignFirstResponder];
}
- (void)targetScan:(UIButton *)scan
{
    // 弹出扫描界面
//    ZBarReaderController *readerVC = [[ZBarReaderController alloc] init];
//    readerVC.readerDelegate = self;
//    // 扫描图片
//    ZBarImageScanner *scanner = readerVC.scanner;
//    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
//    // 展示图片
//    readerVC.showsZBarControls = YES;
//    NSLog(@"3");
//    [self presentViewController:readerVC animated:YES completion:^{
//        
//    }];
}
// 使用图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    // 获取结果
//    id<NSFastEnumeration>results = [info objectForKey:ZBarReaderControllerResults];
//    ZBarSymbol *symbol;
//    //搜索结果
//    for (symbol in results) {
//        break;
//    }
//    // 设置图片
//    self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
//  [picker dismissViewControllerAnimated:YES completion:^{
//  }];
//    self.content.text = symbol.data;
    
}
- (void)targetGo:(UIButton *)gogogo
{
    if (self.content.text.length == 0) {
        self.alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入框内不能为空" delegate:self cancelButtonTitle:@"cancle" otherButtonTitles:@"OK", nil];
        [self.alert show];
    }else {
        [self.dataBase openDB];
        [self.dataBase createQR];
        [self.dataBase insertQR:self.content.text];
        WebQRController *webVC = [[WebQRController alloc] init];
        webVC.result = self.content.text;
        NSLog(@"-------%@", webVC.result);
        [self.navigationController pushViewController:webVC animated:YES];
    }
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
//{
//    WebQRController *webVC = [segue destinationViewController];
//    webVC.result = self.content.text;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//    // Get the new view controller using [segue destinationViewController].
//    // Pass the selected object to the new view controller.
//}


@end
